<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>redundant_keys: common_schema documentation</title>
	<meta name="description" content="redundant_keys: common_schema" />
	<meta name="keywords" content="redundant_keys: common_schema" />
	<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>
	<div id="main">
		<div id="header">
			<h1>common_schema</h1> <strong>2.2</strong> documentation
			<div class="subtitle">DBA's framework for MySQL</div>
		</div>
		<div id="contentwrapper">
			<div id="content">
				<h2><a href="redundant_keys.html">redundant_keys</a></h2>	
<h3>NAME</h3>
redundant_keys: List indexes which are made redundant (or duplicate) by other (dominant) keys. 
<h3>TYPE</h3>
View

<h3>DESCRIPTION</h3>

<p><i>redundant_keys</i> finds and lists keys which are redundant; such that other existing keys can take their functionality, 
or that provide no better selectivity/cardinality than other existing keys.</p>

<p>It is in essence similar to Maatkit's <a href="http://www.maatkit.org/doc/mk-duplicate-key-checker.html">mk-duplicate-key-checker</a>,
but provides information by utulizing a query instead of external scripts.</p>

<p>
Exactly what a redundant or duplicate key is sometimes a matter of perspective. Listed below are sample cases that make or do not make for redundant keys.
The trivial example where a key is redundant is when two identical keys are created. For example: <strong>KEY idx_1 (a), KEY idx_2 (a)</strong>. 
</p>
<p>There is no argument that one of the keys above is redundant. However, the following case is somewhat different:
<strong>KEY idx_1 (a), KEY idx_2 (a, b)</strong>.
</p>

<p>In the above, idx_1 is "covered" by idx_2. Anything idx_1 can do can also be done with idx_2, which indexes column 'a' <i>and then some</i>.</p>

<p>
However, this is not the complete picture. While mathematically being redundant, we may actually desire to explicitly have idx_1 for performance reasons.
Since idx_2 covers more columns, it is more bloated. For queries only searching through 'a', idx_1 may yield with better performance, since scanning through
the index required less I/O (less pages to be scanned). 
</p>

<p>Moreover, for queries where idx_1 makes for a covering index (e.g. <strong>SELECT a FROM t WHERE a BETWEEN 200 AND 500</strong>, 
the difference may be even more significant, since we now only scan the index idx_1 and do not require access to the table.
In the previous example, accessing the table added such overhead that made the difference between the two indexes smaller in comparison to total work.
</p>

<p>The recommendations provided by <i>redundant_keys</i> only refer to the mathematical definition, and leave performance to the discretion of the user.</p>

<p>Terms:
<ul>
	<li><i>KEY</i> and <i>INDEX</i> are synonyms</li>
	<li>A <i>redundant</i> or <i>duplicate</i> index is an index which is not mathematically required.</li>
	<li>A <i>dominant</i> index is a key which makes another index redundant.</li>
</ul>
</p>

<p>
Important notes:
<ul>
	<li>The view provides with a <strong>sql_drop_index</strong> column, making for SQL statement which drop the redundant keys. 
		Do <strong>NOT</strong> take them for granted, or automate them back into MySQL. User is advised to double check all recommendations.</li>
	<li>This view only considers B-Trees. This includes normal InnoDB & MyISAM indexes, and excludes FULLTEXT, HASH, GIS keys.</li>
	<li>Subpart keys (indexing a prefix of a text column, e.g. <strong>KEY `name_idx` (`name` (10))</strong>) are not supported by this view.
		It provides with the <strong>subpart_exists</strong> column, to notify that subpart indexing is in use, but does not validate
		if and how redundancy is affected. User is advised to double & triple check recommendations. See examples below.</li>
	<li>Circular listing of redundant keys may be possible and has not been thoroughly tested. That is, there may be groups of >= 3 keys,
		each making the other redundant, such that the view recommends to drop them all.</li>
</ul>
</p>


<p>
Sample cases where a key is redundant:
<ul>
	<li><strong>KEY idx_1 (a), KEY idx_2 (a)</strong>: the trivial case with two identical keys. Either one can be dropped.</li>
	<li><strong>UNIQUE KEY idx_1 (a), KEY idx_2 (a)</strong>: both index same column, but idx_1 also provides uniqueness. idx_2 is redundant.</li>
	<li><strong>UNIQUE KEY idx_1 (a, b), KEY idx_2 (a, b)</strong>: same as above.</li>
	<li><strong>KEY idx_1 (a), KEY idx_2 (a, b)</strong>: any query utilizing idx_1 can also utilize idx_2. idx_2 can answer for all 'a' issues. 
		This makes idx_1 redundant. See preliminary discussion on explicit redundant keys for more on this.</li>
	<li><strong>KEY idx_1 (a), UNIQUE KEY idx_2 (a, b)</strong>: same as above.</li>
	<li><strong>UNIQUE KEY idx_1 (a), KEY idx_2 (a, b)</strong>: interestingly, idx_2 is redundant. To see why, note that idx_1 is UNIQUE.
	 	Since a UNIQUE key poses a constraint, which is not provided otherwise, idx_1 cannot be redundant.
	 	However, since any 'a' is unique, so is any ('a', 'b') combination. For any 'a' there can only be one 'b', since there can only be one 'a' at most.
	 	This means there is no point in further indexing column 'b' (unless for covering index purposes). There is no added value in terms of
	 	cardinality or selectivity.</li>
	<li><strong>UNIQUE KEY idx_1 (a), UNIQUE KEY idx_2 (a, b)</strong>: continuing the above case, there is no need to declare that ('a', 'b') is UNIQUE,
		since 'a' is known to be unique. It follows that KEY suffices for idx_2. Thus it also follows that idx_2 is redundant.</li>
	<li><strong>KEY idx_1 (a), KEY idx_2 (a(10))</strong>: idx_2 only indexes 1st 10 characters of 'a'. idx_1 indexes all characters.
		idx_2 is redundant.</li>
	<li><strong>KEY idx_1 (a), UNIQUE KEY idx_2 (a(10))</strong>: idx_2 forces 1st 10 characters of 'a' to be UNIQUE. 
		There is no added value to idx_1 (see preliminary discussion). idx_1 is redundant.</li>
</ul>
Sample cases where no key is redundant:
<ul>
	<li><strong>KEY idx_1 (a), KEY idx_2 (b)</strong>: obviously, there's nothing in common between the two keys.</li>
	<li><strong>KEY idx_1 (a), KEY idx_2 (b, a)</strong>: since order of column within an index is important, idx_2 cannot answer for 'a'-only queries
		(except by perhaps providing full index scan, outside our interest, see preliminary discussion). 
		idx_1 therefore answers for queries not solvable by idx_2.</li>
	<li><strong>KEY idx_1 (a, b), KEY idx_2 (b, a)</strong>: both indexes answer for different cases. 
		On some access types, there is some form of waste in definition: in a many-to-many connecting table, where all queries use equality filtering (i.e. 'WHERE a = ?' as opposed to 'WHERE a > ?'),
		idx_1 may suffice with indexing 'a' only. However, with range condition this changes and both keys may be required.</li>
	<li><strong>UNIQUE KEY idx_1 (a, b), KEY idx_2 (b, a)</strong>: same as above.</li>
	<li><strong>UNIQUE KEY idx_1 (a, b), UNIQUE KEY idx_2 (b, a)</strong>: the UNIQUE constraint on either key is not strictly required. 
		However this does not make the index itself redundant. As a side note, UNIQUE constraints are extremely helpful for MySQL query optimizer.</li>
	<li><strong>KEY idx_1 (a), KEY idx_2 (a(10), b)</strong>: 'a' is text column, and idx_2 only indexes 1st 10 characters (subpart index). 
		If any row contains more than 10 characters for 'a', idx_1 provides with indexing not supported by idx_2, and is therefore not redundant.</li>
	<li><strong>UNIQUE KEY idx_1 (a), KEY idx_2 (a(10), b)</strong>: even stricter than the above.</li>
	<li><strong>KEY idx_1 (a), UNIQUE KEY idx_2 (a(10), b)</strong>: idx_2 does NOT force any uniqueness on 'a' itself. 
		It indexes less characters than idx_1. idx_1 is not redundant; nor is idx_2.</li>
</ul>
</p>

<h3>STRUCTURE</h3>

<blockquote><pre>
mysql&gt; DESC common_schema.redundant_keys;
+----------------------------+--------------+------+-----+---------+-------+
| Field                      | Type         | Null | Key | Default | Extra |
+----------------------------+--------------+------+-----+---------+-------+
| table_schema               | varchar(64)  | NO   |     |         |       |
| table_name                 | varchar(64)  | NO   |     |         |       |
| redundant_index_name       | varchar(64)  | NO   |     |         |       |
| redundant_index_columns    | longtext     | YES  |     | NULL    |       |
| redundant_index_non_unique | bigint(1)    | YES  |     | NULL    |       |
| dominant_index_name        | varchar(64)  | NO   |     |         |       |
| dominant_index_columns     | longtext     | YES  |     | NULL    |       |
| dominant_index_non_unique  | bigint(1)    | YES  |     | NULL    |       |
| subpart_exists             | int(1)       | NO   |     | 0       |       |
| sql_drop_index             | varchar(223) | YES  |     | NULL    |       |
+----------------------------+--------------+------+-----+---------+-------+
</pre></blockquote>

<h3>SYNOPSIS</h3>

<p>Columns of this view:</p>
<ul>
	<li><strong>table_schema</strong>: schema of table with redundant index</li>
	<li><strong>table_name</strong>: table with redundant index</li>
	<li><strong>redundant_index_name</strong>: name of index suspected as redundant</li>
	<li><strong>redundant_index_columns</strong>: column covered by redundant index, comma separated, by order of definitions</li>
	<li><strong>redundant_index_non_unique</strong>: <strong>0</strong> if redundant index is UNIQUE; <strong>1</strong> if non-unique</li>
	<li><strong>dominant_index_name</strong>: name of index "covering for" the redundant index. This index is responsible for the redundant index to be redundant.</li>
	<li><strong>dominant_index_columns</strong>: column covered by dominant index, comma separated, by order of definitions</li>
	<li><strong>dominant_index_non_unique</strong>: <strong>0</strong> if dominant index is UNIQUE; <strong>1</strong> if non-unique</li>
	<li><strong>subpart_exists</strong>: <strong>1</strong> if either redundant or dominant keys use string subpart (indexing a prefix of a textual column); this calls for triple-check on the nature of both keys.</li>
	<li><strong>sql_drop_index</strong>: 
		SQL statement to drop redundant index.
		<br/>Use with <a href="eval.html">eval()</a> to apply query.	
	</li>
</ul>

<h3>EXAMPLES</h3>
<p>Detect duplicate keys on sakila.actor:</p>
<blockquote><pre>mysql&gt; ALTER TABLE `sakila`.`actor` ADD INDEX `actor_id_idx` (`actor_id`);

mysql&gt; ALTER TABLE `sakila`.`actor` ADD INDEX `last_and_first_names_idx` (`last_name`, `first_name`);

mysql&gt; ALTER TABLE `sakila`.`film_actor` ADD UNIQUE KEY `film_and_actor_ids_idx` (`film_id`, `actor_id`);

mysql&gt; SELECT * FROM common_schema.redundant_keys \G
*************************** 1. row ***************************
              table_schema: sakila
                table_name: actor
      redundant_index_name: idx_actor_last_name
   redundant_index_columns: last_name
redundant_index_non_unique: 1
       dominant_index_name: last_and_first_names_idx
    dominant_index_columns: last_name,first_name
 dominant_index_non_unique: 1
            subpart_exists: 0
            sql_drop_index: ALTER TABLE `sakila`.`actor` DROP INDEX `idx_actor_last_name`
*************************** 2. row ***************************
              table_schema: sakila
                table_name: actor
      redundant_index_name: actor_id_idx
   redundant_index_columns: actor_id
redundant_index_non_unique: 1
       dominant_index_name: PRIMARY
    dominant_index_columns: actor_id
 dominant_index_non_unique: 0
            subpart_exists: 0
            sql_drop_index: ALTER TABLE `sakila`.`actor` DROP INDEX `actor_id_idx`
*************************** 3. row ***************************
              table_schema: sakila
                table_name: film_actor
      redundant_index_name: idx_fk_film_id
   redundant_index_columns: film_id
redundant_index_non_unique: 1
       dominant_index_name: film_and_actor_ids_idx
    dominant_index_columns: film_id,actor_id
 dominant_index_non_unique: 0
            subpart_exists: 0
            sql_drop_index: ALTER TABLE `sakila`.`film_actor` DROP INDEX `idx_fk_film_id`
</pre></blockquote>

<h3>ENVIRONMENT</h3>
MySQL 5.1 or newer

<h3>SEE ALSO</h3>
<a href="candidate_keys.html">candidate_keys</a>,
<a href="no_pk_innodb_tables.html">no_pk_innodb_tables</a>,
<a href="sql_foreign_keys.html">sql_foreign_keys</a>
<h3>AUTHOR</h3>
Shlomi Noach
				<br/>
			</div>
			<div id="sidebarwrapper">
				<div id="search">
					Search online documentation
					<form id="search_form" name="search_form" method="GET" 
						action="http://www.google.com/search" 
						onsubmit="document.forms['search_form']['q'].value = 'site:http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/ '+document.forms['search_form']['search_term'].value;">
						<input type="text" name="search_term" value=""/>
						<input type="hidden" name="q" value=""/>
						<input type="submit" value="go"/>						
					</form>
				</div>
				<div id="menu">
					<ul>
						<li><a title="Introduction" href="introduction.html">Introduction</a></li>
						<li><a title="Documentation" href="documentation.html">Documentation</a></li>
						<li><a title="Download" href="download.html">Download</a></li>
						<li><a title="Install" href="install.html">Install</a></li>
						<li><a title="Risks" href="risks.html">Risks</a></li>
					</ul>						
					<h3>QUERY SCRIPT</h3>
					<ul>
						<li><a title="QueryScript" href="query_script.html">QueryScript</a></li>
						<li><a title="Execution" href="query_script_execution.html">Execution</a></li>
						<li><a title="Flow control" href="query_script_flow_control.html">Flow control</a></li>
						<li><a title="Statements" href="query_script_statements.html">Statements</a></li>
						<li><a title="Expressions" href="query_script_expressions.html">Expressions</a></li>
						<li><a title="Variables" href="query_script_variables.html">Variables</a></li>
					</ul>						
					<h3>DEBUG</h3>
					<ul>
						<li><a title="rdebug" href="rdebug.html">rdebug</a></li>
						<li><a title="rdebug API" href="rdebug_api.html">rdebug API</a></li>
						<li><a title="rdebug workflow" href="rdebug_workflow.html">Workflow</a></li>
					</ul>						
					<h3>ROUTINES</h3>
					<ul>
						<li><a title="Execution &amp; flow control" href="execution_routines.html">Execution & flow control</a></li>
						<li><a title="General" href="general_routines.html">General</a></li>
						<li><a title="Process" href="process_routines.html">Process</a></li>
						<li><a title="Query analysis" href="query_analysis_routines.html">Query analysis</a></li>
						<li><a title="Schema analysis" href="schema_analysis_routines.html">Schema analysis</a></li>
						<li><a title="Security" href="security_routines.html">Security</a></li>
						<li><a title="Text" href="text_routines.html">Text</a></li>
						<li><a title="Time &amp; date" href="temporal_routines.html">Time & date</a></li>
						<li><a title="Charting" href="charting_routines.html">Charting</a></li>
					</ul>
					<h3>VIEWS</h3>
					<ul>
						<li><a title="Schema analysis" href="schema_analysis_views.html">Schema analysis</a></li>
						<li><a title="Data dimension" href="data_dimension_views.html">Data dimension</a></li>
						<li><a title="Process" href="process_views.html">Process</a></li>
						<li><a title="Security" href="security_views.html">Security</a></li>
						<li><a title="Monitoring" href="monitoring_views.html">Monitoring</a></li>
						<li><a title="InnoDB Plugin" href="innodb_plugin_views.html">InnoDB Plugin</a></li>
						<li><a title="Percona server" href="percona_server_views.html">Percona Server</a></li>
						<li><a title="TokuDB" href="tokudb_views.html">TokuDB</a></li>
					</ul>						
					<h3>DATA</h3>
					<ul>
						<li><a title="tables" href="tables.html">Tables</a></li>
						<li><a title="variables" href="variables.html">Variables</a></li>
					</ul>						
					<h3>META</h3>
					<ul>
						<li><a title="Help" href="help.html">help</a></li>
						<li><a title="Metadata" href="metadata.html">metadata</a></li>
						<li><a title="status" href="status.html">status</a></li>
					</ul>						
				</div>
			</div>	
			<div class="clear">&nbsp;</div>
			
			<div id="footnote" align="center">
				<a href="">common_schema</a> documentation
			</div>
		</div>
	</div>
</body>
</html>
